package org.axeidls.timer;

import java.io.File;
import java.util.List;

import org.axe.annotation.ioc.Autowired;
import org.axe.annotation.ioc.Value;
import org.axe.extra.timer.TimerTask;
import org.axe.util.FileUtil;
import org.axe.util.JsonUtil;
import org.axe.util.LogUtil;
import org.axeidls.component.DataFileComponent;
import org.axeidls.vo.DataBlockInfoPlus;
import org.axeidls.vo.StorageStatus;

/**
 * 空间使用信息统计
 * @author CaiDongyu
 */
public class SpaceInfoTimer extends TimerTask{

	@Value("${data.file-path}")
	private String dataFilePath;
	
	@Autowired
	private DataFileComponent dfc;

	@Value("${server.readonly}")
	private Boolean readonly;
	
	@Override
	public void doSomething() {
		if(readonly) {
			stopAsync();
			LogUtil.info("Readonly, 空间使用信息定时器停止！");
			return;
		}
		try {
			File dataFileFolder = new File(dataFilePath);
			int devicePathIndexStart = dataFileFolder.getAbsolutePath().length();
			File[] listFiles = dataFileFolder.listFiles();
			StorageStatus totalStatus = new StorageStatus();
			for(File file:listFiles) {
				process(file, devicePathIndexStart, totalStatus);
			}
			File dataFilePathFlag = new File(dataFileFolder,"dataFilePath.flag");
			FileUtil.newFile(dataFilePathFlag, JsonUtil.toJson(totalStatus));
		} catch (Exception e) {
			LogUtil.error(e);
		}
	}
	
	private void process(File folder,int devicePathIndexStart,StorageStatus totalStatus) {
		File devicePathFlagFile = new File(folder,"devicePath.flag");
		if(devicePathFlagFile.exists()) {//说明确实是设备数据层
			StorageStatus status = new StorageStatus();
			
			String devicePath = folder.getAbsolutePath().substring(devicePathIndexStart);
			List<String> listDataYearFolder = dfc.listDataYearFolder(devicePath);
			for(String dataYear:listDataYearFolder) {
				List<String> listDataMonthFolder = dfc.listDataMonthFolder(devicePath, dataYear);
				for(String dataMonth:listDataMonthFolder) {
					List<String> listDataDayFolder = dfc.listDataDayFolder(devicePath, dataYear, dataMonth);
					for(String dataDay:listDataDayFolder) {
						List<Integer> listIndex = dfc.listIndex(devicePath, dataYear, dataMonth, dataDay);
						for(int dataBlockIndex:listIndex) {
							try {
								DataBlockInfoPlus dataBlockInfo = dfc.getDataBlockInfo(devicePath, dataYear, dataMonth, dataDay, dataBlockIndex);
								status.addTotalDataNumber(dataBlockInfo.getDataRowCount());
								status.addTotalDataSize(dataBlockInfo.getIndexFileSize() + dataBlockInfo.getDataFileSize());
							} catch (Exception e) {
								if(!e.getMessage().contains("数据文件缺失")) {
									LogUtil.error(e);
								}
							}
						}
					}
				}
			}
			FileUtil.newFile(devicePathFlagFile, JsonUtil.toJson(status));
			
			totalStatus.addTotalDataNumber(status.getTotalDataNumber());
			totalStatus.addTotalDataSize(status.getTotalDataSize());
		} else {
			//未达到数据层
			if(folder.isDirectory()) {
				File[] listFiles = folder.listFiles();
				for(File file:listFiles) {
					process(file,devicePathIndexStart,totalStatus);
				}
			}
		}
	}

	@Override
	public double timeSec() {
		if(readonly) {
			stopAsync();
			LogUtil.info("Readonly, 空间使用信息定时器停止！");
			return Integer.MAX_VALUE;
		}
		return 10;
	}

}
